package priv.conceit.sc.hoxton.file.handle.preview;

import com.aspose.cad.Color;
import com.aspose.cad.fileformats.cad.CadDrawTypeMode;
import com.aspose.cad.imageoptions.CadRasterizationOptions;
import com.aspose.cad.imageoptions.PdfOptions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import priv.conceit.sc.hoxton.common.entity.file.dto.FilePathDTO;
import priv.conceit.sc.hoxton.common.entity.file.dto.FilePreviewDTO;
import priv.conceit.sc.hoxton.common.util.FileUtil;
import priv.conceit.sc.hoxton.common.util.IoUtil;

import java.io.*;


@Component
@Slf4j
public class CadPreviewHandle extends AbsPreviewHandle{

    @Autowired
    DefaultPreviewHandle defaultPreviewHandle;

    /**
     * 根据文件实体 将其处理为对应的文件预览类型
     *
     * @param filePathDTO 文件
     * @param file 文件
     * @return FilePreviewDTO 文件预览dto
     * @author conceit
     * @date 2020/5/15 21:21
     */
    @Override
    public FilePreviewDTO preview(FilePathDTO filePathDTO, File file){
        FilePreviewDTO filePreviewDTO=new FilePreviewDTO();
        filePreviewDTO.setContentType(MediaType.APPLICATION_PDF_VALUE);
        filePreviewDTO.setBytes(IoUtil.toByteArrayUseFileChannel(file));
        return filePreviewDTO;
    }

    /**
     * 根据流对象 将其处理为对应的文件预览类型
     *
     * @param filePathDTO 文件路径信息
     * @return inputStream 流
     * @return FilePreviewDTO 文件预览dto
     * @author conceit
     * @date 2020/5/15 21:21
     */
    @Override
    public FilePreviewDTO preview(FilePathDTO filePathDTO, InputStream inputStream){
        File convertDir=getConvertDir();
        String convertedFileName= FileUtil.replaceFileSuffix(filePathDTO.getSavedFileName(),"pdf");
        File convertFile=new File(convertDir,convertedFileName);

        com.aspose.cad.Image cadImage = com.aspose.cad.Image.load(inputStream);
        CadRasterizationOptions cadRasterizationOptions = new CadRasterizationOptions();
        cadRasterizationOptions.setLayouts(new String[]{"Model"});
        cadRasterizationOptions.setNoScaling(true);
        cadRasterizationOptions.setBackgroundColor(Color.getBlack());
        cadRasterizationOptions.setPageWidth(cadImage.getWidth());
        cadRasterizationOptions.setPageHeight(cadImage.getHeight());
        cadRasterizationOptions.setPdfProductLocation("center");
        cadRasterizationOptions.setAutomaticLayoutsScaling(true);
        cadRasterizationOptions.setDrawType(CadDrawTypeMode.UseObjectColor);
        PdfOptions pdfOptions = new PdfOptions();
        pdfOptions.setVectorRasterizationOptions(cadRasterizationOptions);

        OutputStream stream = null;
        try {
            stream = new FileOutputStream(convertFile);
            cadImage.save(stream, pdfOptions);
            cadImage.close();
        } catch (FileNotFoundException e) {
            log.error("出现cad文件转换异常-> filePathDTO:[{}],fileProperties:[{}]",filePathDTO,fileProperties);
            e.printStackTrace();
            //调用下载方法
            return defaultPreviewHandle.preview(filePathDTO,inputStream);
        }


        FilePreviewDTO filePreviewDTO=new FilePreviewDTO();
        filePreviewDTO.setContentType(MediaType.APPLICATION_PDF_VALUE);
        filePreviewDTO.setBytes(IoUtil.toByteArrayUseFileChannel(convertFile));
        return filePreviewDTO;
    }


}
